package com.example.front.main.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.front.MyApplication;
import com.example.front.R;
import com.example.front.utils.MyAdapter;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 剧本杀界面类
 */
public class ScenarioActivity extends AppCompatActivity {
    private String URL;
    private MyApplication myApplication;
    private ListView scenario_listView;
    private MyAdapter<Scenario> scenarioMyAdapter = null;
    private List<Scenario> scenarioList = null;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scenario);
        myApplication = (MyApplication)getApplication();
        URL = myApplication.getURL();
        scenario_listView = findViewById(R.id.scenario_listView);
        OkHttpUtils.get()
                .url(URL + "/scenario/getScenarioList")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Toast.makeText(ScenarioActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response) {
                        String json = response;
                        Log.i("success", "获取成功！");
                        scenarioList = new ArrayList<Scenario>();
                        try {
                            JSONArray jsonArray = new JSONArray(json);
                            for (int i = 0; i < jsonArray.length(); i++){
                                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                Scenario scenario = new Scenario();
                                scenario.setId(jsonObject.getInt("id"));
                                scenario.setName(jsonObject.getString("name"));
                                scenario.setIntroduction(jsonObject.getString("introduction"));
                                scenario.setUpperLimit(jsonObject.getInt("upperLimit"));
                                scenario.setLowerLimit(jsonObject.getInt("lowerLimit"));
                                scenario.setPosition(jsonObject.getInt("position"));
                                scenario.setPhoto(jsonObject.getString("photo"));
                                scenarioList.add(scenario);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        scenarioMyAdapter = new MyAdapter<Scenario>((ArrayList) scenarioList, R.layout.scenario_list_item){
                            @Override
                            public void bindView(ViewHolder holder, Scenario obj){
                                imageView = holder.getView(R.id.scenario_image);
                                Glide.with(ScenarioActivity.this)
                                        .load(obj.getPhoto())
                                        .error(R.drawable.error)
                                        .crossFade()
                                        .into(imageView);
                                holder.setText(R.id.id_textView, obj.getId() + "");
                                holder.setText(R.id.name_textView, obj.getName());
                                int lowerLimit = obj.getLowerLimit();
                                int upperLimit = obj.getUpperLimit();
                                if(lowerLimit == upperLimit)holder.setText(R.id.type_textView, lowerLimit + "人");
                                else holder.setText(R.id.type_textView, lowerLimit + "-" + upperLimit + "人");
                            }
                        };
                        scenario_listView.setAdapter(scenarioMyAdapter);
                        scenario_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Scenario scenario = scenarioList.get(position);
                                Intent intent = new Intent(ScenarioActivity.this, ScenarioIntroductionActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putString("name", scenario.getName());
                                bundle.putInt("lowerLimit", scenario.getLowerLimit());
                                bundle.putInt("upperLimit", scenario.getUpperLimit());
                                bundle.putString("introduction", scenario.getIntroduction());
                                bundle.putString("photo", scenario.getPhoto());
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        });
                    }
                });
    }
}
